const {
  cityList_mock,
  swiperList_mock,
  gridList_mock,
  categoryList_mock
} = require("../../../mock/index.js")
const {
  houseList_mock
} = require("../../../mock/house.js")
const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //当前城市
    city: '北京',
    //城市列表
    cityList: [],
    //城市列表弹出框
    cityListShow: false,
    //轮播图
    swiperList: [],
    autoplay: true, //是否自动切换
    interval: 2000, //自动切换时间间隔【默认5000】
    indicatorDots: false, //是否显示面板指示点【默认false	】
    indicatorColor: "#ffffff", //指示点颜色【默认rgba(0, 0, 0, .3)】
    indicatorActiveColor: "#20d86e", //当前选中的指示点颜色【默认#000000】
    vertical: false, //滑动方向是否为纵向【默认false】
    duration: 500, //滑动动画时长【默认500】
    circular: true, //是否采用衔接滑动【默认false】
    //宫格图
    gridList: [],
    //广告图
    categoryList: [],
    //房源列表
    houseList: [],
    search: '',
    page: 1,
    size: 10
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.http_swiper()
    this.http_grid()
    this.http_category()
    this.http_house()
    this.setData({
      cityList: cityList_mock,
      //swiperList: swiperList_mock,
      //gridList: gridList_mock,
      //categoryList: categoryList_mock,
      //houseList: houseList_mock
    })
  },
  /**
   * 上拉刷新加载数据
   */
  onReachBottom() {
    //console.log("上拉刷新")
    //老数据合并新数据做累加操作
    this.setData({
      page: this.data.page + 1
    })
    this.http_house()
  },
  /**
   * 城市列表弹出框
   */
  //打开
  onCityListOpen() {
    this.setData({
      cityListShow: true
    })
  },
  //关闭
  onCityListClose() {
    this.setData({
      cityListShow: false
    })
  },
  //切换城市
  onChangeCity(e) {
    this.setData({
      city: e.currentTarget.dataset.cityname
    })
  },
  /**
   * 点击搜索栏
   */
  onSearchTap() {
    wx.navigateTo({
      url: '/pages/search/index/index'
    })
  },
  /**
   * 网络请求
   */
  //查询轮播图
  http_swiper() {
    getRequest(baseUrl + "/front/category/searchByTypeWithStatus", {
        categoryTypeId: 8,
        page: 1,
        size: 10,
      })
      .then(res => {
        console.log(res.data);
        if (res.code == 200) {
          this.setData({
            swiperList: res.data.records,
          })
        }
      })
  },
  //查询宫格
  http_grid() {
    getRequest(baseUrl + "/front/category/searchByTypeWithStatus", {
        categoryTypeId: 9,
        page: 1,
        size: 10,
      })
      .then(res => {
        //console.log(res.data);
        if (res.code == 200) {
          this.setData({
            gridList: res.data.records,
          })
        }
      })
  },
  //查询广告
  http_category() {
    getRequest(baseUrl + "/front/category/searchByTypeWithStatus", {
        categoryTypeId: 10,
        page: 1,
        size: 10,
      })
      .then(res => {
        //console.log(res.data);
        if (res.code == 200) {
          this.setData({
            categoryList: res.data.records,
          })
        }
      })
  },
  //查询房源
  http_house() {
    getRequest(baseUrl + "/front/house/search", {
        search: this.data.search,
        page: this.data.page,
        size: this.data.size,
      })
      .then(res => {
        //console.log(res.data);
        if (res.code == 200) {
          this.setData({
            //老数据合并新数据做累加操作
            houseList: this.data.houseList.concat(res.data.records)
          })
        }
      })
  },



})